/*
 * Copyright (C) 2021 Chinasoft International
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import router from '@system.router';

// 0：采用Ability调用方式; 1：Internal Ability调用方式
const ABILITY_TYPE_EXTERNAL = 0;
// 0：同步 1：异步
const ACTION_SYNC = 0;
//调用模式
const ACTION_MESSAGE_CODE_PLUS = 1001;

export default {
    data: {
    //        展示java端返回的数据
        message: '',
    //        js端传过去的两个数值
        numOne: 0,
        numTwo: 0
    },
//    返回上一个页面
    back() {
        router.back()
    },
//    获取传过去的第一个值
    numOne(e) {
        this.numOne = e.value
    },
//    获取传过去的第二个值
    numTwo(e) {
        this.numTwo = e.value
    },
  /**
   *  js端向java端发送调用请求，并处理返回结果,展示在页面上。
   */
    calculate: async function() {
        var actionData = {};
        actionData.firstNum = this.numOne;
        actionData.secondNum = this.numTwo;

        var action = {};
        action.bundleName = 'com.example.javajscommunication';
        action.abilityName = 'com.example.javajscommunication.ServiceAbility';
        action.messageCode = ACTION_MESSAGE_CODE_PLUS;
        action.data = actionData;
        action.abilityType = ABILITY_TYPE_EXTERNAL;
        action.syncOption = ACTION_SYNC;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        if (ret.code == 0) {
            this.message = '积为:' + JSON.stringify(ret.abilityResult);
        } else {
            this.message = 'java端传回的数据报错' + JSON.stringify(ret.code);
        }
    },
}
